package com.example.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.common.Result;
import com.example.common.ResultCode;
import com.example.entity.Chengji;
import com.example.exception.CustomException;
import com.example.service.ChengjiService;
import com.example.utils.MapWrapperUtils;
import com.example.utils.jwt.JwtUtil;
import com.example.vo.ChengjiVo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

import cn.hutool.poi.excel.ExcelReader;
import org.springframework.web.multipart.MultipartFile;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/chengji")
public class ChengjiController {

    @Resource
    private ChengjiService chengjiService;

    @PostMapping
    public Result<Chengji> add(@RequestBody ChengjiVo chengji) {
		//yoxuxiaxnci
        chengjiService.add(chengji);
        return Result.success(chengji);
    }
	
	
    @GetMapping("/getExcel")
    public void getExcel(HttpServletResponse response) throws IOException {
        // 1. 生成excel
        Map<String, Object> row = new LinkedHashMap<>();
        row.put("xuehao","学号");        row.put("xingming","姓名");        row.put("yuanxi","院系");        row.put("banji","班级");        row.put("zhuanye","专业");        row.put("xuenian","学年");        row.put("qudechengji","取得成绩");        row.put("jingsaihuojiangrenshu","竞赛获奖人数");        row.put("shengjihuojiangrenshu","省级获奖人数");        row.put("guojiajirenshu","国家级人数");        row.put("beizhu","备注");        

        row.put("addtime","添加时间");
        List<Map<String, Object>> list = CollUtil.newArrayList(row);
        List<Map<String, Object>> daochuexcellist = chengjiService.daochuexcel();
        for (Map<String, Object> map : daochuexcellist) {
            list.add(map);
        }
        // 2. 写excel
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.write(list, true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=chaoba.xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(System.out);
    }
	
	@GetMapping("/chengji_tj_yuanxi")    public Result chengji_tj_yuanxi() {        return Result.success(chengjiService.chengji_tj_yuanxi());    }    @GetMapping("/chengji_tj_zhuanye")    public Result chengji_tj_zhuanye() {        return Result.success(chengjiService.chengji_tj_zhuanye());    }    @GetMapping("/chengji_tj_xuenian")    public Result chengji_tj_xuenian() {        return Result.success(chengjiService.chengji_tj_xuenian());    }    @GetMapping("/chengji_tj_jingsaihuojiangrenshu")    public Result chengji_tj_jingsaihuojiangrenshu() {        return Result.success(chengjiService.chengji_tj_jingsaihuojiangrenshu());    }    @GetMapping("/chengji_tj_shengjihuojiangrenshu")    public Result chengji_tj_shengjihuojiangrenshu() {        return Result.success(chengjiService.chengji_tj_shengjihuojiangrenshu());    }    @GetMapping("/chengji_tj_guojiajirenshu")    public Result chengji_tj_guojiajirenshu() {        return Result.success(chengjiService.chengji_tj_guojiajirenshu());    }    

    @PostMapping("/deleteList")
    public Result<Chengji> deleteList(@RequestBody ChengjiVo chengji) {
        chengjiService.deleteList(chengji.getList());
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        chengjiService.delete(id);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody ChengjiVo chengji) {
        chengjiService.update(chengji);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Chengji> detail(@PathVariable Integer id) {
        Chengji chengji = chengjiService.findById(id);
        return Result.success(chengji);
    }

    @GetMapping
    public Result<List<Chengji>> all() {
        return Result.success(chengjiService.list());
    }

    @PostMapping("/page")
    public Result<ChengjiVo> page(@RequestBody ChengjiVo chengjiVo) {
        return Result.success(chengjiService.findPage(chengjiVo));
    }
	
	@PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        List<Chengji> chengjis = reader.readAll(Chengji.class);
		
        chengjiService.saveBatch(chengjis);
        return Result.success();
    }
	
	//youupdt2login
}
